Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Entity
Imports System.Linq
Imports System.Net
Imports System.Web
Imports System.Web.Mvc
Imports System.IO
Imports PagedList
Imports PagedList.Mvc
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering

Public Class <OwnerU>_<TableU>Controller
    Inherits Controller
    Private db As New <Database>DBContext()
        
    ' GET: /<OwnerU>_<TableU>/
    Public Function Index(sortOrder As String,  
                          SearchField As String, 
                          SearchCondition As String,
                          SearchText As String,
                          Export As String,
                          PageSize As Nullable(Of Integer),
                          page As Nullable(Of Integer),
                          command As String) As ActionResult

        If command = "Show All" Then
            SearchField = Nothing
            SearchCondition = Nothing
            SearchText = Nothing
            Session("SearchField") = Nothing
            Session("SearchCondition") = Nothing
            Session("SearchText") = Nothing
        ElseIf command = "Add New Record" Then
            Return RedirectToAction("Create")
        ElseIf command = "Export" Then
            Session("Export") = Export
        ElseIf command = "Search" Or command = "Page Size" Then
            If Not String.IsNullOrEmpty(SearchText) Then
                Session("SearchField") = SearchField
                Session("SearchCondition") = SearchCondition
                Session("SearchText") = SearchText
            End If
        End If
        If command = "Page Size" Then
            Session("PageSize") = PageSize
        End If

    <If Loop>
	        <IsNotText>
	        <And>
	        <IsNotNText>
        ViewData("SearchFields") = GetFields(If(String.IsNullOrEmpty(CStr(Session("SearchField"))) = True, "<FieldC>", CStr(Session("SearchField"))))
        <ExitLoop>
    </If Loop>
        ViewData("SearchConditions") = Library.GetConditions(If(String.IsNullOrEmpty(CStr(Session("SearchCondition"))) = True, "Contains", CStr(Session("SearchCondition"))))
        ViewData("SearchText") = Session("SearchText")
        ViewData("Exports") = Library.GetExports(If(String.IsNullOrEmpty(CStr(Session("Export"))) = True, "Pdf", CStr(Session("Export"))))
        ViewData("PageSizes") = Library.GetPageSizes()

        ViewData("CurrentSort") = sortOrder
    <If Loop>
	        <IsNotText>
	        <And>
	        <IsNotNText>
        ViewData("<FieldU>SortParm") = If(sortOrder = "<FieldU>_asc", "<FieldU>_desc", "<FieldU>_asc")
    </If Loop>

        Dim Query = db.<OwnerU>_<TableU>.AsQueryable()

        Try
            If Not String.IsNullOrEmpty(CStr(Session("SearchField"))) And Not String.IsNullOrEmpty(CStr(Session("SearchCondition"))) And Not String.IsNullOrEmpty(CStr(Session("SearchText"))) Then

                SearchField = CStr(Session("SearchField"))
                SearchCondition = CStr(Session("SearchCondition"))
                SearchText = CStr(Session("SearchText"))

                    If SearchCondition = "Contains" Then
                        Query = Query.Where(Function(p) _ 
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.Value.ToString().Contains(SearchText)) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.Value.ToString().Contains(SearchText)) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) _ 
    </If Loop>
                                         )
                    ElseIf SearchCondition = "Starts with..." Then
                        Query = Query.Where(Function(p) _ 
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.Value.ToString().StartsWith(SearchText)) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<Field>.Value.ToString().StartsWith(SearchText)) _ 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 Or ("<FieldC>".ToString().Equals(SearchField) And p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) _ 
    </If Loop>
                                         )
                    ElseIf SearchCondition = "Equals" Then
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value = m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> = m<Field>)
    </If Loop>
                        End If
                    ElseIf SearchCondition = "More than..." Then
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value > m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> > m<Field>)
    </If Loop>
                        End If
                    ElseIf SearchCondition = "Less than..." Then
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value < m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> < m<Field>)
    </If Loop>
                        End If
                    ElseIf SearchCondition = "Equal or more than..." Then
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value >= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> >= m<Field>)
    </If Loop>
                        End If
                    ElseIf SearchCondition = "Equal or less than..." Then
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        If "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <Convert>(SearchText)
                            Query = Query.Where(Function(p) p.<Field>.Value <= m<Field>)
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        ElseIf "<FieldC>".Equals(SearchField) Then
                            Dim m<Field> = <ConvertRDisplayField>(SearchText)
                            Query = Query.Where(Function(p) p.<ROwner>_<RTable>.<RDisplayField> <= m<Field>)
    </If Loop>
                        End If
                End If
            End If
        Catch ex As Exception
        End Try

        Select Case sortOrder
    <If Loop>
	        <IsNotRelated>
	        <And>
	        <IsNotText>
	        <And>
	        <IsNotNText>
            Case "<FieldU>_desc":
                Query = Query.OrderByDescending(Function(s) s.<FieldU>)
                Exit Select
            Case "<FieldU>_asc":
                Query = Query.OrderBy(Function(s) s.<FieldU>)
                Exit Select
	        <IsRelated>
	        <And>
	        <IsNotText>
	        <And>
	        <IsNotNText>
            Case "<FieldU>_desc":
                Query = Query.OrderByDescending(Function(s) s.<ROwner>_<RTable>.<RDisplayField>)
                Exit Select
            Case "<FieldU>_asc":
                Query = Query.OrderBy(Function(s) s.<ROwner>_<RTable>.<RDisplayField>)
                Exit Select
    </If Loop>
            Case Else
                ' Name ascending 
                Query = Query.OrderBy(Function(s) s.<FieldU>)
                Exit Select
        End Select

        If command = "Export" Then
            Dim gv As New GridView()
            Dim dt As New DataTable()
<Loop>
            dt.Columns.Add("<FieldC>", GetType(String))
</Loop>
            For Each item In Query.ToList()
                dt.Rows.Add(
    <If Loop>
        <IsNotRelated>
        <And>
        <IsCurRow=[0]>
                        item.<FieldU> _
        <IsNotRelated>
        <And>
        <IsCurRow<>[0]>
                       ,item.<FieldU> _
        <IsRelated>
        <And>
        <IsCurRow=[0]>
                        item.<ROwner>_<RTable>.<RDisplayField> _
        <IsRelated>
        <And>
        <IsCurRow<>[0]>
                       ,item.<ROwner>_<RTable>.<RDisplayField> _
    </If Loop>
                    )
            Next
            gv.DataSource = dt
            gv.DataBind()
            ExportData(Export, gv, dt)
        End If

        Dim pageNumber As Integer = (If(page, 1))
        Return View(Query.ToPagedList(pageNumber, CInt((If(Session("PageSize"), 5)))))
    End Function

    ' GET: /<OwnerU>_<TableU>/Details/<id>
    Public Function Details(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                           <FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                          ,<FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                           <FieldU> As <VBType> _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                          ,<FieldU> As <VBType> _
    </If Loop>
                           ) As ActionResult

    <If Loop>
      <IsPKey>
        If _
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
            <FieldU> Is Nothing _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
            Or <FieldU> Is Nothing _
    </If Loop>
    <If Loop>
      <IsPKey>
        Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim <OwnerU>_<TableU> As <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU> _
    </If Loop>
                                            )
    <If Loop>
      <IsPKey>
        If <OwnerU>_<TableU> Is Nothing Then
            Return HttpNotFound()
        End If
        Return View(<OwnerU>_<TableU>)
    End Function
        <ExitLoop>
    </If Loop>

    ' GET: /<OwnerU>_<TableU>/Create
    Public Function Create() As ActionResult
    <If Loop>
      <IsFKey>
        ' ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
        ViewData("<RFieldU>") = New SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>")
    </If Loop>

        Return View()
    End Function

    ' POST: /<OwnerU>_<TableU>/Create
    ' To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    ' more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost> _
    <ValidateAntiForgeryToken> _
    Public Function Create(<Bind(Include:=
		            <If Loop>
                             <IsNotIdentity>
                           <SF[+ "," + ]>"<Field>" _
		            </If Loop>
				  )> <OwnerU>_<TableU> As <OwnerU>_<TableU>) As ActionResult
        If ModelState.IsValid Then
            db.<OwnerU>_<TableU>.Add(<OwnerU>_<TableU>)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
    <If Loop>
      <IsFKey>
        ' ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
        ViewData("<RFieldU>") = New SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>)
    </If Loop>

        Return View(<OwnerU>_<TableU>)
    End Function

    ' GET: /<OwnerU>_<TableU>/Edit/<id>
    Public Function Edit(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                        <FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                       ,<FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                        <FieldU> As <VBType> _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                       ,<FieldU> As <VBType> _
    </If Loop>
                        ) As ActionResult

    <If Loop>
      <IsPKey>
        If _
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
            <FieldU> Is Nothing _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
            Or <FieldU> Is Nothing _
    </If Loop>
    <If Loop>
      <IsPKey>
        Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim <OwnerU>_<TableU> As <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU> _
    </If Loop>
                                            )
    <If Loop>
      <IsPKey>
        If <OwnerU>_<TableU> Is Nothing Then
            Return HttpNotFound()
        End If
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsFKey>
        ' ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
        ViewData("<RFieldU>") = New SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>)
    </If Loop>

    <If Loop>
      <IsPKey>
        Return View(<OwnerU>_<TableU>)
    End Function
        <ExitLoop>
    </If Loop>

    ' POST: /<OwnerU>_<TableU>/Edit/<id>
    ' To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    ' more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost, ActionName("Edit")> _
    <ValidateAntiForgeryToken> _
    Public Function Edit(<OwnerU>_<TableU> As <OwnerU>_<TableU>) As ActionResult

        If ModelState.IsValid Then
            db.Entry(<OwnerU>_<TableU>).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

    <If Loop>
      <IsFKey>
        ' ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
        ViewData("<RFieldU>") = New SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>)
    </If Loop>

        Return View(<OwnerU>_<TableU>)
    End Function

    ' GET: /<OwnerU>_<TableU>/Delete/<id>
    Public Function Delete(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                           <FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                          ,<FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                           <FieldU> As <VBType> _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                          ,<FieldU> As <VBType> _
    </If Loop>
                           ) As ActionResult

    <If Loop>
      <IsPKey>
        If _
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
            <FieldU> Is Nothing _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
            Or <FieldU> Is Nothing _
    </If Loop>
    <If Loop>
      <IsPKey>
        Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim <OwnerU>_<TableU> As <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU> _
    </If Loop>
                                            )
    <If Loop>
      <IsPKey>
        If <OwnerU>_<TableU> Is Nothing Then
            Return HttpNotFound()
        End If
        Return View(<OwnerU>_<TableU>)
    End Function
        <ExitLoop>
    </If Loop>

    ' POST: /<OwnerU>_<TableU>/Delete/<id>
    <HttpPost, ActionName("Delete")> _
    <ValidateAntiForgeryToken> _
    Public Function DeleteConfirmed(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                   <FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                  ,<FieldU> As Nullable(Of <VBType>) _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                   <FieldU> As <VBType> _
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                  ,<FieldU> As <VBType> _
    </If Loop>
                                   ) As ActionResult
    <If Loop>
      <IsPKey>

        Dim <OwnerU>_<TableU> As <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU> _
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU> _
    </If Loop>
                                            )
    <If Loop>
      <IsPKey>
        db.<OwnerU>_<TableU>.Remove(<OwnerU>_<TableU>)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function
        <ExitLoop>
    </If Loop>

    Protected Overrides Sub Dispose(disposing As Boolean)
        If disposing Then
            db.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private Shared Function GetFields(selected As String) As List(Of SelectListItem)
        Dim list As New List(Of SelectListItem)
	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
        Dim Item<iVarA> As SelectListItem = New SelectListItem With {.Text = "<FieldC>", .Value = "<FieldC>"}
        <iVarA=+[1]>
</If Loop>

	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
        <SF[Else ]>If selected = "<FieldC>" Then 
            Item<iVarA>.Selected = True
        <iVarA=+[1]>
</If Loop>
        End If

	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
        list.Add(Item<iVarA>)
        <iVarA=+[1]>
</If Loop>

        Return list.ToList()
    End Function

    Private Sub ExportData(Export As [String], gv As GridView, dt As DataTable)
        If Export = "Pdf" Then
            Dim pdfForm As New PDFform(dt, "<Head>", "Many")
            Dim document As Document = pdfForm.CreateDocument()
            Dim renderer As New PdfDocumentRenderer(True)
            renderer.Document = document
            renderer.RenderDocument()

            Dim stream As New MemoryStream()
            renderer.PdfDocument.Save(stream, False)

            Response.Clear()
            Response.AddHeader("content-disposition", "attachment;filename=" + "Report.pdf")
            Response.ContentType = "application/Pdf.pdf"
            Response.BinaryWrite(stream.ToArray())
            Response.Flush()
            Response.[End]()
        Else
            Response.ClearContent()
            Response.Buffer = True
            If Export = "Excel" Then
                Response.AddHeader("content-disposition", "attachment;filename=" + "Report.xls")
                Response.ContentType = "application/Excel.xls"
            ElseIf Export = "Word" Then
                Response.AddHeader("content-disposition", "attachment;filename=" + "Report.doc")
                Response.ContentType = "application/Word.doc"
            End If
            Response.Charset = ""
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            gv.RenderControl(htw)
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.[End]()
        End If
    End Sub

End Class
